package dao;


import bean.User;
import utils.DbUtil;
import java.util.List;
import java.util.Map;

public class UserDao {
    public static User login(String username, String password) {
        DbUtil dbUtil = DbUtil.getInstance();
        User user = null;
        System.out.println("username = " + username + " password = " + password);
        String querySql = "select * from userinfo where username = ? and password = ?";
        Object[] queryParams = {username, password};

        List<Map<String, Object>> results = dbUtil.queryList(querySql, queryParams);

        if (results != null && !results.isEmpty()) {
            Map<String, Object> result = results.get(0);
            user = new User();
            user.setId((int) result.get("id"));
            user.setUsername((String) result.get("username"));
            user.setPassword((String) result.get("password"));
            user.setSex((Integer) result.get("sex"));
            user.setNote((Integer) result.get("Note"));
            user.setHeadimg((String) result.get("headimg"));
            System.out.println("user = " + user);
            return user;
        }

        return null;
    }
    public static boolean Insert( String username , String password) {

        String note = "1";
        int row=DbUtil.getInstance().execute("insert into userinfo(username,password,Note) values(?,?,?)", new Object[]{username, password, note});
        if(row>0){
            return true;
        }else{
            return false;
        }

    }
    public static boolean Insert( String username, String password, String sex,String role,String filename) {


        int row=DbUtil.getInstance().execute("insert into userinfo(username,password,sex,Note,headimg) values(?,?,?,?,?)", new Object[]{username, password, sex,role,filename});
        if(row>0){
            System.out.println("add成功");
            return true;
        }else{
            System.out.println("add失败");
            return false;
        }

    }
    public static void update(String username, String password,String sex,String filename,String id) {
        System.out.println(username+"~");
        System.out.println(password+"~");
        System.out.println(sex+"~");
        System.out.println(filename+"~");
        System.out.println(id+"~");
        String sql =null;
        Object[] o=null;
        if(filename!=null) {
            System.out.println("qqqq");
            sql = "update userinfo set username=?,password=?,sex=?,headimg = ? where id = ?";
            o =new Object[]{username,password,sex,filename,id};
        }else  {
            System.out.println("pppp");
            sql = "update userinfo set username=?,password=?,sex=? where id = ?";
            o =new Object[]{username,password,sex,id};
        }
        int row = DbUtil.getInstance().execute(sql, o);
        if(row>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
//        int row = DbUtil.getInstance().execute("update userinfo set username=?,password=?,sex=?,headimg = ? where username = ?", new Object[]{username,password,sex,filename,name});
//        if(row>0){
//            System.out.println("修改成功");
//        }else{
//            System.out.println("修改失败");
//        }
    }
    public static void update(String username, String password,String sex,String role,String filename,String id) {
        System.out.println(username);
        System.out.println(password);
        System.out.println(sex);
        System.out.println(role);
        System.out.println(filename);
        String sql =null;
        Object[] o=null;
        if(filename!=null) {
            System.out.println("qqqq");
            sql = "update userinfo set username=?,password=?,sex=?,Note = ?,headimg = ? where id = ?";
             o =new Object[]{username,password,sex,role,filename,id};
        }else  {
            System.out.println("pppp");
             sql = "update userinfo set username=?,password=?,sex=?,Note = ? where id = ?";
             o =new Object[]{username,password,sex,role,id};
        }
        int row = DbUtil.getInstance().execute(sql, o);
        if(row>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }
    public static boolean Delete(int id) {
        int row = DbUtil.getInstance().execute("delete from userinfo where id = ?", new Object[]{id});
        if(row>0){
            return true;
        }else{
            return false;
        }
    }
    public static List<User> select(String message) {
        message = "%"+message+"%";
        return DbUtil.getInstance().queryList("select * from userinfo where id like ? OR username like ? ", new Object[]{message,message}, User.class);

    }
    public static List<User> select() {
        List<User> userlist= (List<User>) DbUtil.getInstance().queryList("select * from userinfo  ", new Object[]{}, User.class);
        for (User user : userlist) {
            System.out.println(user.toString());
        }
        return userlist;

    }
    public static User update(int id){
        return DbUtil.getInstance().queryObject("select * from userinfo where id = ?", new Object[]{id}, User.class);


    }
    public static boolean update1(int id ,String username,String password,String sex,String headimg) {
        int row = DbUtil.getInstance().execute("update userinfo set username=?,password=?,sex=?,headimg = ? where id = ?", new Object[]{username,password,sex,headimg,id});
        if(row>0){
            return true;
        }else{
            return false;
        }
    }
}
